<?php
/**
 * 排期管理，档期管理
 */
namespace Admin\Model;

use Common\Model\WeixinModel;
use Think\Model;
class ScheduleModel extends Model{
	const SUCCESS   =0;//成功
	const FAILURE   =1;//失败

	const BIND_EVENT_NONE    	= 0;//不关联
	const BIND_EVENT_NEW    	= 1;//新建活动
	const BIND_EVENT_OLD    	= 2;//关联老活动

	const SIGN_TYPE_NONE 				=0;//不需报名、
	const SIGN_TYPE_EVENT 				=1;//活动报名、
	const SIGN_TYPE_ARTICLE_COMMENT 	=2;//推文评论、
	const SIGN_TYPE_EVENT_COMMENT 		=3;//活动评论、

	public static function getSignTypeKV(){
		return array(
			self::SIGN_TYPE_NONE 				=>'不需报名',
			self::SIGN_TYPE_EVENT 				=>'活动报名',
			self::SIGN_TYPE_ARTICLE_COMMENT 	=>'推文评论',
			self::SIGN_TYPE_EVENT_COMMENT 		=>'活动评论'
		);
	}


	
	const STATUS_PLAN 		=1;//   1暂定
	const STATUS_SURE		=2;//   2确定
	const STATUS_CANCEL		=3;//   3取消
	public static function getStatusKV(){
		return array(
			self::STATUS_PLAN 		=> '暂定',
			self::STATUS_SURE		=> '确定',
			self::STATUS_CANCEL		=> '取消',
		);
	}

	const PLACE_TOP 		=0;//   头条
	const PLACE_SECOND		=1;//   次一
	const PLACE_THIRD		=2;//   次二
	const PLACE_FOURTH		=3;//   次三
	const PLACE_FIFTH		=4;//   次四
	public static function getPlaceKV(){
		return array(
			self::PLACE_TOP 		=>'头条',
			self::PLACE_SECOND		=>'次一',
			self::PLACE_THIRD		=>'次二',
			self::PLACE_FOURTH		=>'次三',
			self::PLACE_FIFTH		=>'次四'
		);
	}

	const BUSINESS_TYPE_FEE			=1;
	const BUSINESS_TYPE_RESOURCE	=2;
	const BUSINESS_TYPE_MIX			=3;
	const BUSINESS_TYPE_ORIGINAL	=4;
	public static function getBusinessTypeKV(){
		return array(
			self::BUSINESS_TYPE_RESOURCE	=>'资源置换',
			self::BUSINESS_TYPE_FEE			=>'付费',
			self::BUSINESS_TYPE_MIX			=>'付费+资源置换',
			self::BUSINESS_TYPE_ORIGINAL	=>'原创',
		);
	}



	static $articleInstance=null;
	public static function getArticleInstance(){
		if(!self::$articleInstance){
			self::$articleInstance=M('schedule_article',null,DB_MAIN_CFG);
		}
		return self::$articleInstance;
	}

	static $eventInstance=null;
	public static function getEventInstance(){
		if(!self::$eventInstance){
			self::$eventInstance=M('schedule_event',null,DB_MAIN_CFG);
		}
		return self::$eventInstance;
	}


	public static function getUserName(){
		return $adminName = M("gm_sys_user",null,DB_GM_CFG)->getField('real_name',true);
	}

	/**
	 * 取消活动相关推文
	 * @param $eid
	 */
	public static function cancelArticle($eid){
		$date=date('Y-m-d');
		self::getArticleInstance()->where("bindEvent={$eid} and publishDate >= '{$date}'")->setField('status',self::STATUS_CANCEL);
	}


	public static function sendTemplateMsg($timeStr,$itemName,$openID=null){
		if (is_null($openID) || strlen($openID)<10){
			return array('status'=>1,'msg'=>'未指定openid');//'o9VjL1IeF2CLYVgy8GPX8iWJWZqI';
		}
		$templateId=WECHAT_TEMPLATE_SCHEDULE;;
		$url='null';
		$templateMsg=array(
			'touser'=>$openID,
			'template_id'=>$templateId,
			'url'=>$url,
			"topcolor"=>"#FF0000",
			'data'=>array(
				"first"=>array(
					"value"=>"你好，档期管理有时间节点已经到达，请及时处理",
					"color"=>"#173177"
				),
				"keyword1"=>array(
					"value"=>$itemName,
					"color"=>"#173177"
				),
				"keyword2"=>array(
					"value"=>$timeStr,
					"color"=>"#173177"
				),
				"remark"=>array(
					"value"=>"可登陆后台档期管理查看详情",
					"color"=>"#173177"
				),

			)
		);
		$content=json_encode($templateMsg);
		return WeixinModel::sendTemplateMsg($content);
	}
}